package pers.qianyu.handler;

import lombok.extern.slf4j.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.domain.common.*;
import pers.qianyu.exception.*;

/**
 * 全局异常处理
 *
 * @author mizzle rain
 * @date 2020-10-13 17:41
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕捉业务类异常
     *
     * @param e 业务类异常对象
     */
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public HttpResult businessExceptionHandler(BusinessException e) {
        log.error("出现 BusinessException : {}", e.getMessage());
        return HttpResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 捕捉运行时异常
     *
     * @param e 运行时异常对象
     */
    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public HttpResult runtimeExceptionHandler(RuntimeException e) {
        e.printStackTrace();
        log.error("出现 RuntimeException : {}", e.getMessage());
        return HttpResult.error(ErrorCodeEnum.UNKNOWN_ERROR.getCode(), e.getMessage());
    }

    /**
     * 捕捉业务类异常
     *
     * @param e Throwable 对象
     */
    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public HttpResult throwableHandler(Throwable e) {
        log.error("出现 Throwable : {}", e.getMessage());
        return HttpResult.error(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
}
